package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.IndividualHomework;
import orm.Teacher;
import util.DBHelper;

public class IndividualHomeworkDAO {
	
	
	public void insertIHW(Statement statement, IndividualHomework ihw) throws SQLException {
		statement.execute("insert into individualhomework values('" + ihw.getTPID() + "','" + ihw.getAppropriateStudents() + "','" +
				ihw.getHomeworkDate() + "', " + ihw.getTeacherID() + ", '" + ihw.getTeacherName() + "')");
	}
	
	public void updateIHW(Statement statement, IndividualHomework ihw) throws SQLException {
		statement.execute("update individualhomework set AppropriateStudents = '" + ihw.getAppropriateStudents() + 
				"', HomeworkDate = '" + ihw.getHomeworkDate() + "', TeacherID = " + 
				ihw.getTeacherID() + ", TeacherName = '" + ihw.getTeacherName() + 
				"' where TPID = '" + ihw.getTPID() + "'");
	}
	
	public void deleteIHW(Statement statement, String tpID) throws SQLException {
		statement.execute("delete from individualhomework where TPID='" + tpID + "'");
	}
	
	
	public IndividualHomework getIHWByTPID(String tpID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		IndividualHomework ihw = new IndividualHomework();
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from individualhomework where TPID='" + tpID + "'");
		
		while (rs.next()) {
			int i = 1;
			ihw.setTPID(rs.getString(i++));
			ihw.setAppropriateStudents(rs.getString(i++));
			ihw.setHomeworkDate(rs.getString(i++));
			ihw.setTeacherID(rs.getInt(i++));
			ihw.setTeacherName(rs.getString(i++));
		}
		
		rs.close();
		stmt.close();
		conn.close();
		
		return ihw;
	}
	
}
